package org.example.microservice.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.microservice.common.vo.Result;
import org.example.microservice.sys.model.form.DepartmentCreateForm;
import org.example.microservice.sys.model.form.UserSearchForm;
import org.example.microservice.sys.model.po.Department;
import org.example.microservice.sys.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门信息")
@Slf4j
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<List<Department>> list(UserSearchForm form) {
        List<Department> list = departmentService.list();
        return Result.ok(list);
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建部门")
    public Result<Void> create(@RequestBody DepartmentCreateForm form) {
        String message = departmentService.createDepartment(form);
        if (StringUtils.isBlank(message)) {
            return Result.ok();
        }
        return Result.error(message);
    }
}
